package com.shanghaionstar.service;

import android.content.Context;
import android.content.Intent;

import com.onstar.cn.common.util.Constants;
import com.onstar.cn.mag.enumtype.RemoteCommandsType;
import com.onstar.cn.sqlite.entities.PollingEntity;
import com.onstar.cn.sqlite.xutil.DbUtils;
import com.onstar.cn.vehicle.entity.RequestStatus;
import com.onstar.cn.vehicle.service.VehicleDBService;
import com.shanghaionstar.OnstarApplication;

import java.util.List;

/**
 * @author 6335yz
 * @ClassName: VehicleManager
 * @Description: 轮训的入口，提供查询command操作，以此为条件决定是否进行新的command操作
 * @date 2015年8月27日 下午8:01:03
 */
public class VehicleManager {

    private Context context;

    private VehicleDBService dbService;
    private String serverURL;

    /**
     * @Description: 给轮训用
     * @author 6335yz
     * @date 2015年9月8日 上午10:26:52
     */
    public VehicleManager(Context context, String serverURL) {
        this.context = context;
        dbService = new VehicleDBService(context, OnstarApplication.getmDbUtils());
        this.serverURL = serverURL;
    }

    public VehicleManager(Context context) {
        this.context = context;
        dbService = new VehicleDBService(context,OnstarApplication.getmDbUtils());
    }

    public void startPoll(PollingEntity entity) {
        entity.setStartTime(System.currentTimeMillis() + "");
        entity.setRequestStatus(RequestStatus.InProgress.getStringValue());
        dbService.updatePollingStatusByVinAndType(entity);
//		savePollingStatus(entity);
        Intent intent = new Intent(context, VehiclePollingService.class);
        intent.putExtra("pollingEntity", entity);
        intent.putExtra(Constants.SERVER_URL, serverURL);
        context.startService(intent);
    }


    public void initPollingStatusBeforePolling(PollingEntity entity) {
        entity.setRequestStatus(RequestStatus.InProgress.getStringValue());
        savePollingStatus(entity);
    }

    public void updateToFailPollingStatusBeforePolling(PollingEntity entity) {
        entity.setRequestStatus(RequestStatus.Failure.getStringValue());
        dbService.updatePollingStatusByVinAndType(entity);
    }

    private void savePollingStatus(PollingEntity entity) {
        dbService.savePollingStatus(entity);
    }

    /**
     *
     * @Title: savePollingStatusByClick
     * @Description: once click the vehicle command button, then only save the status, vin and type.
     * @param @param entity    设定文件
     * @return void    返回类型
     * @throws
     */
//	public void savePollingStatusByClick(PollingEntity entity) {
//		entity.setRequestStatus(RequestStatus.InProgress.getStringValue());
//		dbService.savePollingStatus(entity);
//	}

    /**
     * @param @param entity    设定文件
     * @return void    返回类型
     * @throws
     * @Title: savePollingStatusByError
     * @Description: compare to savePollingStatusByClick, once error, asve the status as failure.
     */
//	public void savePollingStatusByError(PollingEntity entity) {
//		entity.setRequestStatus(RequestStatus.Failure.getStringValue());
//		dbService.updatePollingStatusByVinAndType(entity);
//	}
    public void insertPollingStatus() {
        String vin = "LSGAR5AL2G4240007";
        PollingEntity start = new PollingEntity();
        start.setVin(vin);
        start.setRequestStatus(RequestStatus.InProgress.getStringValue());
        start.setRequestType(RemoteCommandsType.COMMANDS_START.value());
        dbService.savePollingStatus(start);
        PollingEntity start2 = new PollingEntity();
        start2.setVin(vin);
        start2.setRequestStatus(RequestStatus.InProgress.getStringValue());
        start2.setRequestType(RemoteCommandsType.COMMANDS_LOCK_DOOR.value());
        dbService.savePollingStatus(start2);
    }

    /**
     * @param @param  entity
     * @param @return 设定文件
     * @return boolean    返回类型
     * @throws
     * @Title: checkCommandType
     * @Description: Must execute before commands method in api package.
     */
    public boolean checkCommandType(PollingEntity entity) {
        RemoteCommandsType entityType = RemoteCommandsType.fromValue(entity.getRequestType());
        boolean result = true;
        switch (entityType) {
            case COMMANDS_START:
                //Only two Remote Start functions are allowed in same igntion cycle
                List<PollingEntity> startList = dbService.getInprogressPollingStatusByType(entity.getVin(), RemoteCommandsType.COMMANDS_START.value());
                if (startList.size() >= 2) {
                    result = false;
                }
                break;
            case COMMANDS_CANCEL_START:
                //A Cancel Vehicle Start request will be rejected if the call is made while a Start Vehicle request is in the vehicle queue
                List<PollingEntity> startListInCancle = dbService.getInprogressPollingStatusByType(entity.getVin(), RemoteCommandsType.COMMANDS_START.value());
                if (startListInCancle.size() > 0) {
                    result = false;
                }
                break;
            case COMMANDS_LOCK_DOOR:
                //A Lock Vehicle Door request will be rejected if the call is made while an Unlock Vehicle Door request is in the vehicle queue.
                List<PollingEntity> unLockdoor = dbService.getInprogressPollingStatusByType(entity.getVin(), RemoteCommandsType.COMMANDS_UNLOCK_DOOR.value());
                if (unLockdoor.size() > 0) {
                    result = false;
                }
                break;
            case COMMANDS_UNLOCK_DOOR:
                //A Unlock Vehicle Door request will be rejected if the call is made while an Lock Vehicle Door request is in the vehicle queue
                List<PollingEntity> lockdoor = dbService.getInprogressPollingStatusByType(entity.getVin(), RemoteCommandsType.COMMANDS_LOCK_DOOR.value());
                if (lockdoor.size() > 0) {
                    result = false;
                }
                break;
            default:
                result = true;
                break;
        }
        return result;
    }

    /**
     * @param @return 设定文件
     * @return boolean    返回类型
     * @throws
     * @Title: isInProgressCommand
     * @Description: 给外部提供检查是否有正在polling的车辆操作
     */
    public boolean isInProgressCommand(String vin, RemoteCommandsType commandsType) {
        List<PollingEntity> entities = null;
//		if(CommonUtil.isNull(commandsType)) {
//			entities = dbService.getInprogressPollingStatus(vin);
//		}else {//用于车辆位置检查或是车况刷新
//			entities = dbService.getInprogressPollingStatusByType(vin, commandsType.value());
//		}
        entities = dbService.getInprogressPollingStatus(vin);
        return !(entities == null || entities.size() == 0);
    }

    /**
     * @param 设定文件
     * @return void    返回类型
     * @throws
     * @Title: refreshPollingStatus
     * @Description: 当意外退出车辆操作或程序意外崩溃时候更新polling的车辆操作为fail
     */
    public void refreshPollingStatus(String vin) {
        dbService.updatePollingStatusToFail(vin);
    }
}
